<?php

namespace Frieza\Modules\Position\Controllers;

use Doraemon\pockets\constInterface\PopupServiceConf;
use Doraemon\service\Popups\PopupsApi;
use Encore\Admin\Grid;
use Encore\Admin\Widgets\Form;
use Illuminate\Http\Request;


class HomePopupsController extends BaseController
{
    use BaseTrait;

    public function saveModule(Request $request, $baseInfoId)
    {
        $base = $request->input('base', []);
        $rules = $request->input('rules', []);
        $popupsData = $request->input('popups', []);
        $id = $base['id'] ?? 0;
        $copy_id = $base['copy_id'] ?? 0;
        $img_size = $rules['img_size'] ?? 0;
        $pic = $popupsData['bgPic'] ?? '';
        if ($popupsData['type'] == PopupServiceConf::STYLE_CHECK_LINK) {

        } else {
            if (empty($pic)) throw new \Exception('图片必须上传');
            if (($id > 0 || $copy_id > 0) && $img_size == 0) {

            } else {
                $this->rulesValid($rules);
            }
        }

        if ($id == 0) unset($popupsData['id']);

        PopupsApi::savePopups($popupsData, $baseInfoId);
    }

    public function extendList(Grid $grid)
    {
        $grid->column('img_url', '弹窗主图')->image("", 100);
    }

    public function defaultBaseInfo(Request $request)
    {
        $response = parent::defaultBaseInfo($request);

        $response['pos'] = $response['pos'] ?: 1;

        return $response;
    }

    public function extendDefaultBaseInfo()
    {
        return [
            'popups' => [
                'type' => PopupServiceConf::STYLE_FLOATING
            ],
        ];
    }

    public function defaultUserProfile()
    {
//        return [
//            'action_all' => Action::PART_DAY,
//            'action' => [1],
//        ];
    }

    public function beforeTitleForm(Form $form)
    {

    }

    public function beforeLinkForm(Form $form)
    {
        $form->customPopups('popups', '弹窗配置', $form, $this->imgHelp());
    }

}


